
package tptecnicas.logging;

import java.io.PrintWriter;
import java.io.StringWriter;

import tptecnicas.configuration.ConfigProvider;
import tptecnicas.configuration.Configuration;
import tptecnicas.configuration.LogConfig;
import tptecnicas.logging.filters.Filter;
import tptecnicas.logging.filters.FiltersManager;

/**
 * Clase que implementa la interfaz publica al usuario.
 * Administra la comunicacion entre los distintos modulos de la herramienta.
 */
public class LoggerManager implements Logger {

	private ConfigProvider configProvider;
	private LogOuputManager writeController;
	private Configuration configuration;
	private String LoggerName;
	private FiltersManager filtersManager;

	public LoggerManager(String name) {
		this.configProvider = new ConfigProvider();
		configuration = this.configProvider.getConfiguration();
		this.writeController = new LogOuputManager();
		this.filtersManager = new FiltersManager();
		this.LoggerName= name;
		for(LogConfig config : configuration.getLogConfigList()) {
			writeController.addWriter(WriterFactory.getWriter(config));
		}
	}
	
	@Override
	public void logMessage(String level, String message) {
		MessageInfo logMessage = new MessageInfo(message, level, configuration.getSeparator(), this.LoggerName);
		if (!filtersManager.shouldBeFiltered(logMessage)) {
			this.writeController.write(logMessage);
		}
	}	
	
	@Override
	public void logMessageWithException(String level, String message, Throwable ex) {
		StringWriter sw = new StringWriter();
		PrintWriter pw = new PrintWriter(sw);
		ex.printStackTrace(pw);
		this.logMessage(message.concat(sw.toString()), level);
	}	
	
	@Override
	public void configureNewFileWriter(String target, String level, String formatPattern) {
		LogConfig config = new LogConfig(target, level, formatPattern);
		this.writeController.addWriter(WriterFactory.getFileWriter(config));
	}

	@Override
	public void configureConsoleWriter(String level, String formatPattern) {
		LogConfig config = new LogConfig("Terminal", level, formatPattern);
		this.writeController.addWriter(WriterFactory.getConsoleWriter(config));	
	}


	@Override
	public void configureNewCustomWriter(String customWriterConfigFile, String level, String formatPattern) {
		this.writeController.addWriter(WriterFactory.getCustomWriter(new LogConfig(customWriterConfigFile, level, formatPattern)));
	}
	
	@Override
	public void deactivateFileWriter(String fileTarget) {
		this.writeController.pause(WriterFactory.getFileWriter(fileTarget));
	}

	@Override
	public void deactivateConsoleWriter() {
		this.writeController.pause(WriterFactory.getConsoleWriter());
	}

	@Override
	public void activateFileWriter(String fileTarget) {
		this.writeController.unPause(WriterFactory.getFileWriter(fileTarget));
	}

	@Override
	public void activateConsoleWriter() {
		this.writeController.unPause(WriterFactory.getConsoleWriter());
	}
	
	public void setSeparator(String separator) {
		configuration.setSeparator(separator);
	}
		
	@Override
	public String getLoggerName() {
		return LoggerName;
	}

	public void addFilter(Filter filter){		
		filtersManager.addFilter(filter);
	}

	@Override
	public boolean isLevelEnabled(String level) {
		return this.writeController.isLevelEnabled(level);
	}

}
